package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoAsyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author 李旭
 * @date 2021/11/27 14:32
 * @Description:
 *
 *    Spring开启子线程
 *
 *
 */
@Service
public class CartInfoAsyncServiceImpl implements CartInfoAsyncService {


    @Autowired
    private CartInfoMapper cartInfoMapper;


    @Override
    @Async   //开启子线程
    public void updateAsync(CartInfo cartInfo) {
        System.out.println("updateAsync:子线程名称:" + Thread.currentThread().getName());
        //cartInfoMapper.updateById(cartInfo);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
          .eq("user_id",cartInfo.getUserId())
          .eq("sku_id",cartInfo.getSkuId()));
    }

    @Override
    @Async   //开启子线程
    public void insertAsync(CartInfo cartInfo) {
        System.out.println("insertAsync:子线程名称:" + Thread.currentThread().getName());
        cartInfoMapper.insert(cartInfo);
    }

    //批量的将购物车数据 临时用户 更新成真实用户的数据
    @Override
    @Async
    public void updateAsyncByUserId(String userTempId, String userId) {
        System.out.println("updateAsync:子线程名称:" + Thread.currentThread().getName());
        // update cart_info set user_id = 真实用户ID where user_id = 临时用户的Id
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
            .eq("user_id",userTempId));
    }



    //删除
    @Override
    @Async
    public void deleteByUserIdAndSkuId(CartInfo cartInfoByUserTempId) {
        cartInfoMapper.delete(new QueryWrapper<CartInfo>()
          .eq("sku_id",cartInfoByUserTempId.getSkuId())
           .eq("user_id",cartInfoByUserTempId.getUserId()));
    }
    //单独修改 临时用户为真实用户ID
    @Override
    public void updateAsyncByUserTempIdAndSkuId(CartInfo cartInfoByUserTempId, String userTempId) {
        cartInfoMapper.update(cartInfoByUserTempId,new QueryWrapper<CartInfo>()
         .eq("sku_id",cartInfoByUserTempId.getSkuId())
          .eq("user_id",userTempId));

    }
}
